/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Dell 1564
 */
public class ServerList {
    private Collection<Notify> threadList =
	            new ArrayList<Notify>();
	private int counter = 0;

	public synchronized void add(Notify item) {
		try {
			while (counter > 0) {
				wait();
			}
			threadList.add(item);
	    }
	    catch (InterruptedException e) {
	    	System.out.println("Addition interrupted.");
	    }
	    finally{
	        notifyAll();
	    }
	}

	public synchronized void remove(Notify item) {
	    try {
			while (counter > 0) {
				wait();
			}
			threadList.remove(item);
		}
	    catch (InterruptedException e) {
	    	System.out.println("Removal interrupted.");
	    }
	    finally {
	        notifyAll();
	    }
	}

	public synchronized void incCounter() {
		counter++;
		notifyAll();
	}

	public synchronized void decCounter() {
		counter--;
		notifyAll();
	}

	public Collection getCollection() {
		return threadList;
	}
}
